let express = require("express");
let uunewid = require("uuid");
let router = express.Router();

const { ohana } = require("ohana-node-orm");
const tableName = "SAP_JIANGDU_SUBSCRIBE";
const ohana_instance = new ohana(tableName);

const { checkAuth } = require("../utils/utils");
let constants = require("../utils/constants");

router.get("/", function (req, res, next) {
  // #swagger.tags = ['订阅']
  // #swagger.summary = '获取列表'
  // #swagger.description = '需要JWT Token'
  /* #swagger.security = [{
                 "JiangduJWT": []
    }] */
  const user = checkAuth(req.user, constants.BASICAUTH);

  if (!user.authCheck) {
    res.sendStatus(403);
  }

  let payload = {};
  payload.USER_ID = user.user_id;

  getData(payload)
    .then((result) => {
      res.send(result);
    })
    .catch((err) => {
      console.log(err);
    });
});

router.post("/", function (req, res, next) {
  // #swagger.tags = ['订阅']
  // #swagger.summary = '插入列表'
  /* #swagger.security = [{
                 "JiangduJWT": []
    }] */
  /*  #swagger.requestBody = {
                required: true,
                content: {
                    "application/json": {
                        schema: {
                            $ref: "#/definitions/subscribe_add"
                        }  
                    },
                }
        } */
  let { data } = req.body;
  const user = checkAuth(req.user, constants.BASICAUTH);

  if (!user.authCheck) {
    res.sendStatus(403);
  }

  data.USER_ID = user.user_id;
  insertData(data)
    .then((result) => {
      res.status(200).send(result);
    })
    .catch((err) => {
      console.log(err);
    });
});

router.put("/", function (req, res, next) {
  // #swagger.tags = ['订阅']
  // #swagger.summary = '更新列表'
  /* #swagger.security = [{
                 "JiangduJWT": []
    }] */
  /*  #swagger.requestBody = {
                required: true,
                content: {
                    "application/json": {
                        schema: {
                            $ref: "#/definitions/subscribe_update"
                        }  
                    },
                }
        } */
  const { data } = req.body;
  updateData(data)
    .then((result) => {
      if (result == 1) {
        res.sendStatus(200);
      } else {
        res.sendStatus(500);
      }
    })
    .catch((err) => {
      console.log(err);
    });
});

router.delete("/", function (req, res, next) {
  // #swagger.tags = ['订阅']
  // #swagger.summary = '删除列表'
  /* #swagger.security = [{
                 "JiangduJWT": []
    }] */
  /*  #swagger.requestBody = {
                required: true,
                content: {
                    "application/json": {
                        schema: {
                            $ref: "#/definitions/subscribe_delete"
                        }  
                    },
                }
        } */
  const { data } = req.body;
  deleteData(data)
    .then((result) => {
      if (result == 1) {
        res.sendStatus(200);
      } else {
        res.sendStatus(500);
      }
    })
    .catch((err) => {
      console.log(err);
    });
});

async function getData(condition) {
  let result = [];
  result = await ohana_instance.find(condition);
  return result;
}

async function insertData(data) {
  data.SUBSCRIBE_ID = uunewid.v4();
  let result = await ohana_instance.insert(data);
  return data.SUBSCRIBE_ID;
}

async function updateData(data) {
  let result = await ohana_instance.update(
    {
      SUBSCRIBE_ID: data.SUBSCRIBE_ID,
    },
    data
  );
  return result;
}

async function deleteData(data) {
  let result = ohana_instance.raw(
    'delete from "' +
    tableName +
    "\" where SUBSCRIBE_ID = '" +
    data.SUBSCRIBE_ID +
    "'"
  );
  return result;
}

module.exports = router;
